package cn.yr.system.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.yr.common.core.domain.entity.SysDictData;
import cn.yr.common.core.domain.vo.PageVo;
import cn.yr.common.exception.ret.Ret;
import cn.yr.system.service.ISysDictDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName SysDictDataController
 * @description 数据字典信息
 * @author Lang
 * @date 2020-07-13 11:23
 */
@RestController
@RequestMapping("/sys/dict/data")
public class SysDictDataController {
	@Autowired
	private ISysDictDataService dictDataService;

	@RequestMapping("/list")
	public Ret list(@RequestBody PageVo<SysDictData> pageVo){
		Map<String, Object> params = pageVo.getParams();
		QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
		wrapper.eq(ObjectUtil.isNotEmpty(params.get("status")),"status",params.get("status"));
		wrapper.like(ObjectUtil.isNotEmpty(params.get("dictLabel")),"dict_label",params.get("dictLabel"));
		wrapper.like(ObjectUtil.isNotEmpty(params.get("dictType")),"dict_type",params.get("dictType"));
		wrapper.orderByDesc("create_time");
		PageVo<SysDictData> page = dictDataService.page(pageVo, wrapper);
		return Ret.success(page);
	}

	/**
	 * 查询字典数据详细
	 */
	@GetMapping(value = "info/{dictCode}")
	public Ret getInfo(@PathVariable Long dictCode) {
		return Ret.success(dictDataService.selectDictDataById(dictCode));
	}

	/**
	 * 根据字典类型查询字典数据信息
	 */
	@GetMapping(value = "/dictType/{dictType}")
	public Ret dictType(@PathVariable String dictType) {
		return Ret.success(dictDataService.selectDictDataByType(dictType));
	}

	/**
	 * 新增字典类型
	 */
	@RequestMapping("add")
	public Ret add(@Validated @RequestBody SysDictData dict) {
		return Ret.success(dictDataService.insertDictData(dict));
	}

	/**
	 * 修改保存字典类型
	 */
	@RequestMapping("edit")
	public Ret<Integer> edit(@Validated @RequestBody SysDictData dict) {
		return Ret.success(dictDataService.updateDictData(dict));
	}

	/**
	 * 删除字典类型
	 */
	@RequestMapping("remove/{dictCodes}")
	public Ret<Integer> remove(@PathVariable Long[] dictCodes) {
		return Ret.success(dictDataService.deleteDictDataByIds(dictCodes));
	}
}
